// pages/myPublish/myPublish.js
const call = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myPublishes: []
  },

  toEdit: function(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/addPublish/addPublish?id=' + id,
    })
  },

  changeStatus:function(e) {
    const that = this;
    const openId = wx.getStorageSync('token')
    const id = e.currentTarget.dataset.id
    const myPublishes = this.data.myPublishes
    call.requestGet('jus/openJob',{
      openId: openId,
      id: id
    },function(e){
      console.log(e)
      // 请求成功，用这个id定位列表中的那个兼职并更改它的状态
      for (let i in myPublishes) {
        if (id == myPublishes[i].id) {
          that.setData({
            ["myPublishes[" + i + "].open"]: !that.data.myPublishes[i].open
          })
          return
        }
      }
    },function(e){
      wx.showToast({
        title: '哎哟～前方拥堵',
        duration: 2000
      })
    })
  },

  toDelete: function (e) {
    const that = this; 
    const openId = wx.getStorageSync('token')
    const id = e.currentTarget.dataset.id
    wx.showModal({
      title: '确认删除吗',
      content: '删除后不可恢复',
      confirmColor: '#99ccff',
      success(res) {
        if (res.confirm) {
          call.requestGet('jus/deleteJob', {
            openId: openId,
            id: id
          }, function(e) {
            console.log(e)
            that.getmyPublishes()
          }, function () {})
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  
  getmyPublishes: function () {
    const that = this;
    wx.showLoading({
      title: '加载中',
    })
    const openId = wx.getStorageSync('token')
    call.requestGet('jus/findAllPublish', {
      openId: openId
    }, function (e) {
      console.log('myPublish', e)
      that.setData({
        myPublishes: e
      })
      wx.hideLoading()
    }, function () {
      wx.hideLoading()
      wx.showToast({
        title: '哎哟～前方拥堵',
        duration: 2000
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getmyPublishes()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})